import { useRef, useEffect } from 'react'

function useTimeout() {
	const timeoutRef = useRef<NodeJS.Timeout>()
	useEffect(() => {
		return () => clearTimeout(timeoutRef.current)
	}, [])
	const startTimeout = (fn?: Function, duration: number = 2000) => {
		clearTimeout(timeoutRef.current)
		timeoutRef.current = setTimeout(() => {
			fn && fn()
			clearTimeout(timeoutRef.current)
		}, duration)
	}
	return [startTimeout, timeoutRef] as const
}
export default useTimeout
